package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Bounce;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	public class Player implements ISerializable
	{
		private var m_testTimer:Timer = new Timer(1000);
		private var m_citizens:Vector.<Citizen> = new Vector.<Citizen>();
		private var m_gameObjects:Vector.<GameObject> = new Vector.<GameObject>();
		private var m_currentMoney:Number = 0;
		private var m_lastActionTime:int;
		
		private var m_debugDisplay:Sprite = new Sprite();
		private var m_debugTextField:TextField = new TextField();
		
		private var m_switchedPathingStrategy:Boolean = false;
		
		public function Player()
		{
			m_debugTextField.width = 500;
			m_testTimer.addEventListener(TimerEvent.TIMER, onTestTick);
			m_testTimer.start();
			
			m_debugDisplay.addChild(m_debugTextField);
		}
		
		public function serialize():Object{
			var serializedObject:Object = new Object();
			serializedObject.citizens = Citizen.serializeVector(m_citizens);
			serializedObject.gameObjects = GameObject.serializeVector(m_gameObjects);
			
			return serializedObject;
		}
		
		public function wakeup(serData:Object):void{
			Citizen.unserializeVector(serData.citizens, m_citizens);
			GameObject.unserializeVector(serData.gameObjects, m_gameObjects );
			
			for each(var gameObject:GameObject in m_gameObjects){
				Global.isoScene.addChild(gameObject.isoDisplay);
			}
		}
		
		public function changeCitizenPathingStrategy():void{
			m_switchedPathingStrategy = true;
			var i:int = 0, length:int = m_citizens.length;
			for(i = 0; i < length; i++){
				m_citizens[i].switchToPathingDestinationStrategy();
			}
		}
		
		public function getDebugDisplay():Sprite{
			return m_debugDisplay;
		}
		
		private function onTestTick(te:TimerEvent):void{
			var deltaTime:int = getTimer() - m_lastActionTime;
			var debugText:String = '';
			var moneyRate:Number = getCurrentMoneyRate();
			debugText += "Money Rate: " + moneyRate + "\n";
			debugText += "Citizens: " + m_citizens.length + "\n" ;
			debugText += "Stores: " + m_gameObjects.length + "\n" ;
			debugText += "Est $ / day: " + moneyRate*60*60*24 + "\n" ;
			debugText += "Total $: " + getTotalMoney(deltaTime);
			m_debugTextField.text = debugText;
		}
		
		public function buyCitizen(citizen:Citizen):void{
			if(m_switchedPathingStrategy){
				citizen.switchToPathingDestinationStrategy();
			}
			
			updateSimulation();
			Global.isoScene.addChild(citizen.isoDisplay);
			Global.isoScene.addChild(citizen.waypointDisplay);
			Global.simulationManager.add(citizen);
			m_citizens.push(citizen);
		}
		
		public function buy(gameObject:GameObject):void{
			updateSimulation();
			
			m_currentMoney -= 50;
			m_gameObjects.push(gameObject);
			gameObject.isoDisplay.z = 10*Constants.SQUARE_SIZE;
			gameObject.isoDisplay.container.alpha = 0;
			TweenLite.to(gameObject.isoDisplay, 1, {z:0, ease:Bounce.easeOut});
			TweenLite.to(gameObject.isoDisplay.container, .5, {alpha: 1});
			gameObject.onBuyFinished();

		}
		
		private function updateSimulation():void{
			var curTime:int = getTimer();
			var deltaTime:int = curTime - m_lastActionTime;
			updateCurrentMoney(deltaTime);
			m_lastActionTime = curTime;
		}
		
		private function updateCurrentMoney(deltaTime:int):void{
			m_currentMoney += getCurrentMoneyRate()*deltaTime/1000;
		}
		
		public function getTotalMoney(deltaTime:int):Number{
			return getCurrentMoneyRate()*deltaTime/1000;
		}
		
		public function getCurrentMoneyRate():Number{
			var moneyRate:Number = 0;
			var numCitizens:Number = m_citizens.length;
			var numStores:Number = m_gameObjects.length;
			var numEverything:Number = numCitizens*numStores;
			var storeCustomerRatio:Number;
			
			
			if(numCitizens && numStores){
				storeCustomerRatio = Math.min(numCitizens / numStores, numStores / numCitizens);	
			} else {
				storeCustomerRatio = 0;
			}
			
			for(var i:int=0, l:int=numCitizens; i<l; i++){
				moneyRate += 1 / Math.pow(2, i/numStores);
			}
			
			return moneyRate;
		}
		
	}
}